﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserveapi.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserveapi.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserveapi.codeplex.com/license
 */

using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace MCServe_API_for_Windows
{
    public enum Commands
    {
        EMPTY,                              /* Basic API Test */
        WEBSITE,                            /* Returns a lsit of all registered websites */
        SERVERVERSION                       /* Returns the current version of MCServe Server */
    }

    public class MCServeApi
    {
        private String _ApiUrl = "";
        private Boolean _IsDev = true;
        private String _Command = ApiCommands.BASICCHECK;
        private ApiValues _Values = new ApiValues();

        public MCServeApi()
            : this(false)
        {
        }
        public MCServeApi(Boolean dev)
        {
            if (_IsDev)
            {
                _ApiUrl = "http://localhost:62048/api/{0}";
            }
            else
            {
                _ApiUrl = "http://api.mcserve.org/api/{0}";
            }
        }

        public ApiResult Query(String cmd, ApiValues val)
        {
            String CommandUri = "";
            _Command = cmd;
            _Values = val;

            if (_Values.Count() > 0)
            {
                CommandUri = String.Format(_ApiUrl, _Command);

                for (Int32 i = 0; i <= (_Values.Count() - 1); i++)
                {
                    if (i == 0)
                    {
                        CommandUri += "?";
                    }
                    
                    CommandUri += _Values.GetKeyByIndex(i + 1) + "=" + _Values.GetValueByIndex(i + 1);

                    if (i != (_Values.Count() - 1))
                    {
                        CommandUri += "&";
                    }
                }

            }
            else
            {
                CommandUri = String.Format(_ApiUrl, _Command);
            }

            WebClient wClient = new WebClient();
            try
            {
                return new ApiResult(true, wClient.DownloadString(CommandUri));
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    return new ApiResult(false, ((HttpWebResponse)e.Response).StatusCode.ToString());
                }
                else
                {
                    return new ApiResult(false, "UNKNOWN ERROR");
                }
            }
        }
    }
}
